package org.apache.servicecomb.samples.practise.houserush.realestate.service;

import org.apache.servicecomb.samples.practise.houserush.realestate.dao.RealestateDao;
import org.apache.servicecomb.samples.practise.houserush.realestate.entity.Realestate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RealestateServiceImpl implements  RealestateService {

    @Autowired
    private RealestateDao realestateDao;

    @Override
    public Realestate createRealesate(Realestate realestate) {
        return realestateDao.save(realestate);
    }

    @Override
    public Realestate findRealestate(Integer id) {
        return realestateDao.findOne(id);
    }

    @Override
    public Realestate updateRealestate(Realestate realestate) {
        int id = realestate.getId();
        if(realestateDao.exists(id)) {
            return realestateDao.save(realestate);
        } else {
            throw new DataRetrievalFailureException("数据不存在，无法修改");
        }
    }

    @Override
    public List<Realestate> indexRealestates() {
        return realestateDao.findAll();
    }

    @Override
    public void removeRealestate(int id) {
        if(realestateDao.exists(id)) {
            realestateDao.delete(id);
        } else {
            throw new DataRetrievalFailureException("您要删除的数据不存在，无法删除");
        }
    }
}
